﻿package CutLoad.components
{
	import CutLoad.*;
	import CutLoad.components.container.CScrollV;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 * 多行文本，带有滚动条
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CTextArea extends Base
	{
		private var txt:CTextField
		private var myTextFied:TextField;
		private var scroll:CScrollV;
		private var fmt:TextFormat
		/**
		 * 返回 一个默认设置的CTextArea
		 * @param w 宽度 
		 * @param h 高度
		 * @return
		 * 
		 */		
		public static function getSimple(w:Number=10,h:Number=10):CTextArea{
			var txt:CTextArea=new CTextArea();
			txt.init(w,h);
			return txt;
		}
		/**
		 * 初始化
		 * @param w 宽
		 * @param h 高
		 * @param fmt 文本类型
		 * @param offsetSpeed 缓存动速度
		 * @param isShowUpDownBtn 是否显示上下移动按钮
		 * @param numSeed 上下移动的速度
		 * 
		 */		
		public function init(w:Number,h:Number,fmt:TextFormat=null,offsetSpeed:Number=0.3,isShowUpDownBtn:Boolean=true,numSeed:Number=5):void{
			txt=new CTextField();
			if(fmt==null){
				fmt=CTextField.getTextFormat(0,0x000000,12);
			}
			txt.init("",fmt,true);
			myTextFied=txt.getTextField();
			myTextFied.autoSize=TextFieldAutoSize.LEFT;
			myTextFied.wordWrap=true;
			this.fmt=fmt;
			//
			scroll=new CScrollV(txt,w,h,offsetSpeed,isShowUpDownBtn,numSeed);
			this.addChild(scroll);
		}
		public function setEdit(isEdit:Boolean=true):void{
			if(isEdit){
				myTextFied.type=TextFieldType.INPUT;
				myTextFied.multiline=true;
				//获得行高
				myTextFied.text="";
				var hangHeight:Number=myTextFied.textHeight;
				var hangCountTem:Number=Math.ceil(this.getShowHeight()/hangHeight);
				//显示一些等高的空行，让用户可以点到这些区域
				var strTem123:String="";
				for(var i:Number=0;i<hangCountTem;i++){
					strTem123=myTextFied.text+"\n";
				}
				myTextFied.text=strTem123;
				//
				myTextFied.addEventListener(FocusEvent.FOCUS_IN,txtFocusinout);
				myTextFied.addEventListener(Event.CHANGE,txtChage);
			}else{
				myTextFied.multiline=false;
				myTextFied.type=TextFieldType.DYNAMIC
				if(myTextFied.hasEventListener(Event.CHANGE)){
					myTextFied.removeEventListener(Event.CHANGE,txtChage);
					myTextFied.removeEventListener(FocusEvent.FOCUS_IN,txtFocusinout);
				}
			}			
		}
		private function txtFocusinout(e:FocusEvent):void{
			if(myTextFied.textHeight<this.getShowHeight()){
				var hangCountTem:Number=Math.ceil(this.getShowHeight()/getLineHeight());
				hangCountTem-=myTextFied.numLines;
				//显示一些等高的空行，让用户可以点到这些区域
				var strTem123:String="";
				for(var i:Number=0;i<hangCountTem;i++){
					strTem123=myTextFied.text+"\n";
				}
				myTextFied.text=strTem123;
			}
		}
		private function txtChage(e:Event):void{
			scroll.refresh();
		}
		/**
		 * 获得行高 
		 * @return 
		 * 
		 */		
		public function getLineHeight():Number{
			return myTextFied.textHeight/myTextFied.numLines;
		}
		/**
		 * 获得文本框 
		 * @return 
		 * 
		 */		
		public function getTextField():TextField{
			return txt.getTextField();
		}
		/**
		 * 返回文字域 的CTextField
		 * @return 
		 * 
		 */		
		public function getCTextField():CTextField{
			return this.txt
		}
		/**
		 * 设置文本内容 
		 * @param str
		 * 
		 */		
		public function set text(str:String):void{
			txt.text=str;
			scroll.refresh();
		}
		/**
		 * 设置文本内容 
		 * @param str
		 * 
		 */		
		public function appendText(str:String):void{
			myTextFied.appendText(str);
			scroll.refresh();
			scroll.moveBarEnd();
		}
		/**
		 * 返回滚动条的引用 
		 * @return 
		 * 
		 */		
		public function getScroll():CScrollV{
			return scroll;
		}
		/**
		 * 滚动条向上或向下 
		 * @param t 格式：只能是:1向上,2向下
		 * @param MultipleSpeed 速度的倍数移动
		 * @param isAddY 是否在滚动y基础上相加。
		 * 
		 */		
		public function moveBar(t:Number=1,MultipleSpeed:Number=1,isAddY:Boolean=true):void{
			getScroll().moveBar(t,MultipleSpeed,isAddY);
		}
		/**
		 * 获得文本内容 
		 * @return 
		 * 
		 */		
		public function get text():String{
			return txt.text
		}
		/**
		 * 设置文本html内容 
		 * @param str
		 * 
		 */		
		public function set htmlText(str:String):void{
			setHtmlTextNoRefresh(str)
			scroll.refresh();
		}
		/**
		 * 设置文本html内容，但 不刷新滚动条
		 * @param str
		 * 
		 */		
		public function setHtmlTextNoRefresh(str:String):void{
			if(!myTextFied.multiline) myTextFied.multiline=true;
			txt.setTextFormat(this.fmt,false);
			myTextFied.htmlText=str;
		}
		/**
		 * 追加 文本html内容 
		 * @param str
		 * 
		 */		
		public function appendHtmlText(str:String):void{
			if(!myTextFied.multiline) myTextFied.multiline=true;
			myTextFied.htmlText=myTextFied.htmlText+str;
			scroll.refresh();
			scroll.moveBarEnd();
		}
		//
		//覆盖------------------------------------------
		override public function setWH(w:Number=0, h:Number=0):void{
			scroll.setWH(w,h);
		}
		override public function getShowWidth():Number{
			return scroll.getShowWidth();
		}
		override public function getShowHeight():Number{
			return scroll.getShowHeight();
		}
		override public function killMe():void{
			myTextFied.removeEventListener(Event.CHANGE,txtChage);
			super.killMe();
		}
		
	}
}